package cn.sykj.car.controller;


import cn.sykj.car.common.ResponseBean;
import cn.sykj.car.common.StatusCode;
import cn.sykj.car.dict.DeleteFlagEnum;
import cn.sykj.car.entity.ConfigRecharge;
import cn.sykj.car.entity.Users;
import cn.sykj.car.service.IConfigRechargeService;
import cn.sykj.car.service.IUsersService;
import cn.sykj.car.utils.Utils;
import cn.sykj.car.vo.ConfigRechargeVO;
import cn.sykj.car.vo.ResponseMessageVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 * 充值设置 前端控制器
 * </p>
 *
 * @author xuming
 * @since 2021-06-25
 */
@RestController
@RequestMapping("/configRecharge")
@Slf4j
public class ConfigRechargeController extends BaseController {

    @Resource
    private IConfigRechargeService iConfigRechargeService;
    @Resource
    private IUsersService iUsersService;

    DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 添加充值设置信息
     * *
     *
     * @param configRecharge
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "添加充值设置信息", notes = "添加充值设置信息")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("configRecharge:create")
    @RequestMapping(value = "/addConfigRecharge", method = RequestMethod.POST)
    public ResponseBean addConfigRecharge(@RequestBody ConfigRecharge configRecharge) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        Users user = iUsersService.getUserByName((String) obj);

        configRecharge.setCreateTime(new Date());
        configRecharge.setCreateUser(user.getUserName());
        configRecharge.setUpdateTime(new Date());
        configRecharge.setUpdateUser(user.getUserName());
        configRecharge.setDelFlag(DeleteFlagEnum.FALSE.VALUE);


        ResponseMessageVO responseMessageVO = iConfigRechargeService.addConfigRecharge(configRecharge);
        if (Utils.notEmpty(responseMessageVO) && responseMessageVO.isRetFlag()) {
            this.writeLog2Manager(responseMessageVO);
        }
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }

    /**
     * 删除充值设置信息
     *
     * @param id
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "删除充值设置信息", notes = "删除充值设置信息")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("configRecharge:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public ResponseBean deleteConfigRecharge(@PathVariable Integer id) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        ResponseMessageVO responseMessageVO = iConfigRechargeService.deleteConfigRecharge(id);
        if (Utils.notEmpty(responseMessageVO) && responseMessageVO.isRetFlag()) {
            this.writeLog2Manager(responseMessageVO);
        }
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }

    /**
     * 查询充值设置信息
     *
     * @param configRechargeVO
     * @param currentPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "查询充值设置信息", notes = "查询充值设置信息")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("configRecharge:view")
    @RequestMapping(value = "/pageConfigRecharge", method = RequestMethod.POST)
    public ResponseBean pageConfigRecharge(@RequestBody ConfigRechargeVO configRechargeVO,
                                           @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer currentPage,
                                           @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) throws Exception {


        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        //分页查询全部记录
        if (pageSize == 0) {
            pageSize = Integer.MAX_VALUE;
        }

        IPage<ConfigRecharge> page = new Page(currentPage, pageSize);
        QueryWrapper<ConfigRecharge> queryWrapper = new QueryWrapper<>();

        if (Utils.notEmpty(configRechargeVO.getType())) {
            queryWrapper.eq(ConfigRecharge.TYPE, configRechargeVO.getType());
        }

        if (Utils.notEmpty(configRechargeVO.getOverflyFlag())) {
            queryWrapper.eq(ConfigRecharge.OVERFLY_FLAG, configRechargeVO.getOverflyFlag());
        }

        if (Utils.notEmpty(configRechargeVO.getUpdateTimeStart())) {
            // 四位年-两位月-两位日 两位小时：两位分钟：两位秒
            queryWrapper.ge(ConfigRecharge.UPDATE_TIME, sdf.parse(configRechargeVO.getUpdateTimeStart()));
        }
        if (Utils.notEmpty(configRechargeVO.getUpdateTimeEnd())) {
            queryWrapper.le(ConfigRecharge.UPDATE_TIME, sdf.parse(configRechargeVO.getUpdateTimeEnd()));
        }
        queryWrapper.orderByDesc(ConfigRecharge.UPDATE_TIME);

        page = iConfigRechargeService.page(page, queryWrapper);
        return new ResponseBean(StatusCode.OK, page);
    }

    /**
     * 修改充值设置信息
     *
     * @param configRecharge
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "修改充值设置信息", notes = "修改充值设置信息")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("configRecharge:edit")
    @RequestMapping(value = "/updateConfigRecharge", method = RequestMethod.POST)
    public ResponseBean updateConfigRecharge(@RequestBody ConfigRecharge configRecharge) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        // 当前登录人的获取
        Users user = iUsersService.getUserByName((String) obj);
        configRecharge.setUpdateTime(new Date());
        configRecharge.setUpdateUser(user.getUserName());

        ResponseMessageVO responseMessageVO = iConfigRechargeService.updateConfigRecharge(configRecharge);
        this.writeLog2Manager(responseMessageVO);
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }
}
